home *** CD-ROM | disk | FTP | other *** search
/ InterCD 2000 September / september_2000.iso / intercd / root / ^Linux / cdrtools-1.10 / lib / stdio / fileread.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-04-20  |  2.1 KB  |  95 lines

  1. /* @(#)fileread.c    1.8 97/04/20 Copyright 1986 J. Schilling */
  2. /*
  3.  *    Copyright (c) 1986 J. Schilling
  4.  */
  5. /*
  6.  * This program is free software; you can redistribute it and/or modify
  7.  * it under the terms of the GNU General Public License as published by
  8.  * the Free Software Foundation; either version 2, or (at your option)
  9.  * any later version.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  * 
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program; see the file COPYING.  If not, write to
  18.  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  */
  20.  
  21. #include <stdio.h>
  22. #include <errno.h>
  23. #include "io.h"
  24.  
  25. static    char    _readerr[]    = "file_read_err";
  26.  
  27. #ifdef    HAVE_USG_STDIO
  28.  
  29. int fileread(f, buf, len)
  30.     register FILE    *f;
  31.     void    *buf;
  32.     int    len;
  33. {
  34.     int    cnt;
  35.     register int    n;
  36.  
  37.     down2(f, _IOREAD, _IORW);
  38.  
  39.     if (f->_flag & _IONBF){
  40.         cnt = _niread (fileno(f), buf, len);
  41.         if (cnt < 0) {
  42.             f->_flag |= _IOERR;
  43.             if (!(my_flag(f) & _IONORAISE))
  44.                 raisecond(_readerr, 0L);
  45.         }
  46.         if (cnt == 0 && len)
  47.             f->_flag |= _IOEOF;
  48.         return cnt;
  49.     }
  50.     cnt = 0;
  51.     while (len > 0) {
  52.         if (f->_cnt <= 0){
  53.             if (_filbuf(f) == EOF)
  54.                 break;
  55.             f->_cnt++;
  56.             f->_ptr--;
  57.         }
  58.         n = f->_cnt >= len ? len : f->_cnt;
  59.         buf = (void *)movebytes (f->_ptr, buf, n);
  60.         f->_ptr += n;
  61.         f->_cnt -= n;
  62.         cnt += n;
  63.         len -= n;
  64.     }
  65.     if (!ferror(f))
  66.         return cnt;
  67.     if (!(my_flag(f) & _IONORAISE))
  68.         raisecond(_readerr, 0L);
  69.     return -1;
  70. }
  71.  
  72. #else
  73.  
  74. int fileread(f, buf, len)
  75.     register FILE    *f;
  76.     void    *buf;
  77.     int    len;
  78. {
  79.     int    cnt;
  80.  
  81.     down2(f, _IOREAD, _IORW);
  82.  
  83.     if (my_flag(f) & _IOUNBUF)
  84.         return _niread(fileno(f), buf, len);
  85.     cnt = fread(buf, 1, len, f);
  86.  
  87.     if (!ferror(f))
  88.         return cnt;
  89.     if (!(my_flag(f) & _IONORAISE))
  90.         raisecond(_readerr, 0L);
  91.     return -1;
  92. }
  93.  
  94. #endif    /* HAVE_USG_STDIO */
  95.